package com.alibaba.citrus.cr.mn.order.center.facade.function.asynctask;

import com.alibaba.citrus.cr.common.enums.ordercenter.AsyncMessageEventEnum;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.BeanConverter;
import com.epoch.app.crb2btradecenter.messagetask.model.MessageTask;
import com.epoch.app.crb2btradecenter.model.enums.MessageTaskTypeEnum;
import com.epoch.app.mnordercenter.model.dto.CommonAsyncMessageResponse;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCreateResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName OrderAsyncTaskListenerStrategySelector
 * @description: TODO
 * @date 2023/06/06 11:13
 * @version: 1.0
 */
public interface OrderAsyncTaskListenerConsumerSelector {

    MessageTaskTypeEnum getMessageTaskType();

    void doConsumer(String ossTaskContent, MessageTask messageTask);

    void sendOccupyInventoryOccupyMsg(List<String> bizOrderId);

    default CommonAsyncMessageResponse convertCommonAsyncMessageResponse(
            String batchTaskId, String fromSys, String businessUnitCode,
            List<OrderAtomCreateResponse> createResponses) {
        if (CollectionUtils.isEmpty(createResponses)) {
            return null;
        }

        AsyncMessageEventEnum event;
        switch (getMessageTaskType()) {
            case OC_SALES_ORDER_BATCH_CREATE:
            case OC_DIRECT_SALES_ORDER_BATCH_CREATE:
                event = AsyncMessageEventEnum.SALES_ORDER_CREATE;
                break;
            case OC_RETURN_ORDER_BATCH_CREATE:
                event = AsyncMessageEventEnum.RETURN_ORDER_CREATE;
                break;
            case OC_EXCHANGE_ORDER_BATCH_CREATE:
                event = AsyncMessageEventEnum.EXCHANGE_ORDER_CREATE;
                break;
            default:
                return null;
        }

        List<CommonAsyncMessageData> commonAsyncMessageDataList = createResponses.stream().map(p -> {

            CommonAsyncMessageData commonAsyncMessageData = new CommonAsyncMessageData();
            BeanConverter.convert(p, commonAsyncMessageData);

            commonAsyncMessageData.setSuccess(StringUtils.isBlank(p.getErrorCode()));
            commonAsyncMessageData.setBatchTaskId(batchTaskId);

            return commonAsyncMessageData;
        }).collect(Collectors.toList());

        CommonAsyncMessageResponse commonAsyncMessageResponse = new CommonAsyncMessageResponse();
        commonAsyncMessageResponse.setEventCode(event.getValue());
        commonAsyncMessageResponse.setEventName(event.getLabel());
        commonAsyncMessageResponse.setEventTime(String.valueOf(new Date().getTime()));
        commonAsyncMessageResponse.setFromSys(fromSys);
        commonAsyncMessageResponse.setBusinessUnitCode(businessUnitCode);
        commonAsyncMessageResponse.setData(commonAsyncMessageDataList);
        return commonAsyncMessageResponse;
    }
}
